package com.dyw.ycgovfinance.mvp.news.newshome;

import android.support.annotation.NonNull;

import com.dyw.ycgovfinance.bean.NewsKindBean;
import com.dyw.ycgovfinance.commom.Config;
import com.dyw.ycgovfinance.commom.TipStr;
import com.dyw.ycgovfinance.http.HttpService;
import com.dyw.ycgovfinance.http.HttpServiceUtil;
import com.dyw.ycgovfinance.util.CalendarUtil;
import com.dyw.ycgovfinance.util.CommonUtil;
import com.dyw.ycgovfinance.util.LogUtil;
import com.dyw.ycgovfinance.util.schedulers.BaseSchedulerProvider;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/6/20.
 */

public class NewsHomePresenter implements NewsHomeContract.Presenter {


    @NonNull
    NewsHomeContract.View mView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private final HttpService mHttpService;

    @NonNull
    private CompositeSubscription mSubscriptions;


    public NewsHomePresenter(@NonNull NewsHomeContract.View view, @NonNull BaseSchedulerProvider mSchedulerProvider
            , @NonNull HttpService mHttpService) {
        this.mView = view;
        this.mSchedulerProvider = mSchedulerProvider;
        this.mHttpService = mHttpService;
        this.mSubscriptions = new CompositeSubscription();

        this.mView.setPresenter(this);
    }







    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.unsubscribe();
        mSubscriptions = null;
    }


    @Override
    public void getNewsKind() {

        mView.showLoading();

        Subscription subscription = mHttpService.getNewsKind(HttpServiceUtil.makeRequestBody(makeParams()))
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        LogUtil.e("newsKind:" + jsonObject.toString());
                        if (!jsonObject.get(HttpServiceUtil.RESOIBSE_KEY_CODE).getAsString().equals(HttpServiceUtil.RESOIBSE_STATE_SUCCESS)) {
                            mView.dismissLoading();
                            mView.onGetNewsKindError(jsonObject.get(HttpServiceUtil.RESOIBSE_KEY_MESSAGE).getAsString());

                        }
                        return jsonObject.get(HttpServiceUtil.RESOIBSE_KEY_CODE).getAsString().equals(HttpServiceUtil.RESOIBSE_STATE_SUCCESS);
                    }
                })
                .subscribe(new Subscriber<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mView.dismissLoading();
                        mView.onGetNewsKindError(TipStr.TIP_SERVER_GET_DATA_WRONG);
                    }

                    //onNext是当执行结果正确的时候执行的地方
                    @Override
                    public void onNext(JsonObject jsonObject) {

                        List<NewsKindBean> newsKindBeanList = CommonUtil.formatJsonToObject(jsonObject,"lmcodeList",new TypeToken<List<NewsKindBean>>(){});

                        mView.dismissLoading();
                        mView.onGetNewsKindSuccess(newsKindBeanList);
                    }
                });
    }



    public Map<String, String> makeParams() {
        String nowTime = CalendarUtil.calendar2String3(Calendar.getInstance());

        List<String> signParamsList = new ArrayList<>();
        signParamsList.add("deviceType");
        signParamsList.add("time");


        List<String> signValuesList = new ArrayList<>();
        signValuesList.add(Config.DEVICE_TYPE);
        signValuesList.add(nowTime);

        LogUtil.e("deviceType:"+Config.DEVICE_TYPE);
        LogUtil.e("time:"+nowTime);

        String signStr = HttpServiceUtil.getSignValue(signParamsList, signValuesList);

        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("sign", signStr);
        requestParams.put("deviceType", Config.DEVICE_TYPE);
        requestParams.put("time", nowTime);

        return (HashMap<String, String>) requestParams;
    }
}

